<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary">增加停车计费规则</el-button>
      <el-button @click="exportExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号"/>
        <el-table-column label="计费规则编号" prop="ruleNumber"/>
        <el-table-column label="计费规则名称" prop="ruleName"/>
        <el-table-column label="免费时长(分钟)" prop="freeDuration"/>
        <el-table-column label="收费上线(元)" prop="chargeCeiling"/>
        <el-table-column label="计费方式">
          <template #default="scope">
            {{ scope.row.chargeType }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView"/>
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div class="page-container">
        <el-pagination
          layout="total,prev, pager, next"
          :current-page="params.page"
          :page-size="params.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getRuleListAPI } from '@/api/rule'
import { utils, writeFileXLSX } from 'xlsx'

export default {
  name: 'Building',
  data() {
    return {
      ruleList: [],
      params: {
        page: 1,
        pageSize: 2
      },
      total: 0,
      dialogVisible: false
    }
  },
  mounted() {
    this.getRuleList()
  },
  methods: {
    async getRuleList() {
      const res = await getRuleListAPI(this.params)
      this.ruleList = res.data.rows
      this.total = res.data.total
    },
    handleCurrentChange(page) {
      this.params.page = page
      this.getRuleList()
    },
    async exportExcel() {
      // 创建一个新的 工作簿
      const workBook = utils.book_new()
      // 创建一个新的 工作表
      const res = await getRuleListAPI(this.params)
      const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      const sheetData = res.data.rows.map(item => {
        const newObj = {}
        tableHeaderKeys.forEach(key => {
          newObj[key] = item[key]
        })
        return newObj
      })
      const workSheet = utils.json_to_sheet(sheetData)
      // 将工作表添加到工作簿中  Data 为工作表名称
      utils.book_append_sheet(workBook, workSheet, '停车计费规则')
      // 改写表头
      utils.sheet_add_aoa(workSheet, [tableHeaderValues], { origin: 'A1' })
      // 导出文件
      writeFileXLSX(workBook, '停车计费规则.xlsx')
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}

.page-container {

}
</style>
